﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;


namespace caBoxingUnboxingDemo
{
    class BoxingUnboxing
    {
        static void Main(string[] args)
        {

            int num = 100;
            Object obj = num;
            // num = 400;
            int numafter = (int)obj;
            Console.WriteLine("boxing。。。。。。。。。。unboxing。。。。。");
            Console.WriteLine("num={0}\t obj={2}\t numafter={2}", num, obj, numafter);


            short s1 = 1;

            s1 = Convert.ToInt16(s1 + 1);

            Console.WriteLine("\n\n\n\n");

            string source = "Hello";
            using (MD5 md5Hash = MD5.Create())
            {
                string hash = GetMD5Hash(md5Hash, source);
                Console.WriteLine("The MD5 hash of "+ source + " is: " + hash + ".");
                Console.WriteLine("Verifying the hash...");

                if (VerifyMd5Hash(md5Hash, "Hello World", hash))
                {
                    Console.WriteLine("The hashes are the same.");
                }
                else
                {

                    Console.WriteLine("The hashes are not same.");
                }
            }


            Console.ReadKey();
        }

        static string GetMD5Hash(MD5 md5Hash, String src)
        {
            StringBuilder sb = new StringBuilder();
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(src));
            foreach (byte item in data)
            {
                sb.Append(item.ToString("x2"));
            }
            return sb.ToString();
        }

        static bool VerifyMd5Hash(MD5 md5Hash, string input, string srcHash)
        {
            string hashOfInput = GetMD5Hash(md5Hash, input);
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;
            return comparer.Compare(hashOfInput, srcHash) == 0 ? true : false;  
        }

    }
}
